package com.irdstudio.tdp.console.api.rest;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.tdp.console.utils.JsonUtil;
import com.irdstudio.sdk.beans.core.util.KeyUtil;
import com.irdstudio.sdk.beans.core.vo.EasyUITreeData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.irdstudio.tdp.console.service.facade.AppModelTreeService;
import com.irdstudio.tdp.console.service.vo.AppModelTreeVO;
import com.irdstudio.sdk.beans.core.vo.ResponseData;
import com.irdstudio.sdk.ssm.web.AbstractController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api")
public class AppModelTreeController extends AbstractController  {
	
	@Autowired
	@Qualifier("appModelTreeServiceImpl")
	private AppModelTreeService appModelTreeService;

	
	/**
	 * 列表数据查询
	 * @return
	 */
	@RequestMapping(value="/app/model/trees", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<AppModelTreeVO>> queryAppModelTreeAll(
			AppModelTreeVO vo) {		
		List<AppModelTreeVO> outputVo = appModelTreeService.queryAllOwner(vo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/app/model/tree/{packageId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<AppModelTreeVO> queryByPk(@PathVariable("packageId") String packageId) {		
		AppModelTreeVO inVo = new AppModelTreeVO();
				inVo.setPackageId(packageId);
		AppModelTreeVO outputVo = appModelTreeService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param inAppModelTreeVo
	 * @return
	 */
	@RequestMapping(value="/app/model/tree", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody AppModelTreeVO inAppModelTreeVo) {		
		int outputVo = appModelTreeService.deleteByPk(inAppModelTreeVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inAppModelTreeVo
	 * @return
	 */
	@RequestMapping(value="/app/model/tree", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody AppModelTreeVO inAppModelTreeVo) {		
		int outputVo = appModelTreeService.updateByPk(inAppModelTreeVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inAppModelTreeVo
	 * @return
	 */
	@RequestMapping(value="/app/model/tree", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertAppModelTree(@RequestBody AppModelTreeVO inAppModelTreeVo) {
		inAppModelTreeVo.setPackageId(KeyUtil.createUUIDKey());
		int outputVo = appModelTreeService.insertAppModelTree(inAppModelTreeVo);
		return getResponseData(outputVo);
		
	}

	@GetMapping(value = "/app/model/tree/nav/{appId}")
	public void queryTreeSWithAppId(@PathVariable("appId") String appId, HttpServletResponse response) {
		AppModelTreeVO vo = new AppModelTreeVO();
		vo.setAppId(appId);
		EasyUITreeData treeData = appModelTreeService.queryTreeData(vo);
		// 以JSON格式输出信息
		StringBuffer jsonBuf = new StringBuffer();

		jsonBuf.append(JsonUtil.MULTI_BEGIN);
		jsonBuf.append(JSONObject.toJSONString(treeData));
		jsonBuf.append(JsonUtil.MULTI_END);

		response.setContentType("text/html; charset=utf-8");
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			pw.write(JsonUtil.formatJson(jsonBuf.toString()));
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@PostMapping(value = "/app/model/tree/nav/{appId}/catalog")
	public @ResponseBody List<EasyUITreeData> queryTreeSWithAppTree(@PathVariable("appId") String appId, AppModelTreeVO appModelTreeVo) {
		appModelTreeVo.setAppId(appId);
		List<EasyUITreeData> treeData = appModelTreeService.queryTreeDataWithSingle(appModelTreeVo);
		return treeData;
	}
}
